@model SealManagement.Models.User

@{
ViewData["Title"] = "添加管理员";
}
<h2 style="margin:20px 0">添加管理员</h2>

<script src="~/js/utils.js"></script>
<div class="form-horizontal">
	<h4>请插入管理员UKEY后点击按钮进行添加</h4>
	<hr />
	<dl class="dl-horizontal">
		<dt></dt>
		<dd>
			<h4 id="h4Title" class="text-danger" style="display: none">请稍等，正在进行UKEY验证……</h4>
		</dd>
	</dl>
	<div class="form-group">
		<div id="waitingPicDiv" class="col-md-offset-2 col-md-10" style="display: none">
			<img src="~/Content/waiting_transparent.gif" />
		</div>
	</div>
	<div class="form-group">
		<div class="col-md-offset-2 col-md-6">

			<form class="form-horizontal">
				<div class="form-group">
					<label for="inputEmail3" class="col-sm-3 control-label">用户名</label>
					<div class="col-sm-9">
						<input type="text" class="form-control" id="UserName" name="UserName">
					</div>
				</div>
				<div class="form-group">
					<label for="inputPassword3" class="col-sm-3 control-label">备注</label>
					<div class="col-sm-9">
						<input type="text" class="form-control" id="Remark" name="Remark">（无特殊说明可空）
					</div>
				</div>
				@*新修改部分*@
				<div class="form-group">

					<div class="col-sm-offset-2 col-sm-10" style="display:none">
						<input type="text" class="form-control" id="ArrayNo" name="ArrayNo" placeholder="certSN">
					</div>
					<div class="col-sm-offset-2 col-sm-10" style="display:none">
						<input type="text" class="form-control" id="Certificator" name="Certificator" placeholder="certBase64">
					</div>

					<label for="ukeySelect" class="col-sm-3 control-label">请选择UKEY</label>
					<div class="col-sm-9" style="line-height: 30px;">
						<select id="ukeySelect" name="ukeySelect" lay-filter="ukeySelect" class="form-control"></select>
					</div>
					<div class="col-sm-offset-6 col-sm-10" style="margin-top:20px;">
						<a><input type="button" class="btn btn-default" value="添加管理员" id="btnCreate" /></a>
					</div>
				</div>
			</form>

		</div>

	</div>
</div>
<dl id="infoDiv" class="dl-horizontal" style="display: none">
	<dt>@*当前进度：*@</dt>
	<dd></dd>
</dl>
<div id="debug" style="display: none"></div>


@section scripts {
<script src="/Content/layui/layui.js" charset="utf-8"></script>
<script src="~/js/dateformat.js"></script>
<script>
	function initlist(str) {
		if (str == "") {
			$("select[name='ukeySelect']").empty();
		}
		//转换成js对象并赋值
		let obj = $.parseJSON(str);
		//使失去焦点
		$("select[name='ukeySelect']").blur();
		//清空子元素
		$("select[name='ukeySelect']").empty();
		//遍历对象生成节点
		$(obj.CertInfolist).each(function(index, value) {
			$("select[name='ukeySelect']").append("<option value='" + value.CertID + "'>" + value.CertCN + "</option>")
		})

	}

	function myPost(url, args) {
		var body = $(document.body),
			form = $("<form method='post' id='myform'></form>"),
			input;
		form.attr({
			"action": url
		});
		$.each(args, function(key, value) {
			input = $("<input type='hidden'>");
			input.attr({
				"name": key
			});
			input.val(value);
			form.append(input);
		});

		form.appendTo(document.body);

		var params = $("#myform").serialize();
		console.log(params);
		$.post(url, params, function(e) {
			console.log(e);
			if (e.state == 200) {
				layer.msg("成功添加管理员");
				window.location.href = '/User/Index';
			} else {
				layer.msg(e.message);
			}
		})

		if (FormData != null) {
			//FormData.
		}
		document.body.removeChild(form[0]);
	}

	//新修改部分
	$(function() {
		EnumKey(function(data) {
				initlist(data);
			},
			function() {
				layui.use('layer',
					function() {
						var layer = layui.layer;
						layer.msg("未安装UKEY支持服务！");
					});
			});
		//新修改部分
		$('#btnCreate').click(function() {
			console.log("@ViewBag.ValidateCode");
			var base64 = new Base64();
			// console.log($("#ukeySelect").val());
			var certSN = $("#ukeySelect").val();
			//获取证书和印章
			GetCertAndSeal(certSN, function(certAndSeal) {
				var certAndSealJObject = JSON.parse(certAndSeal);
				if (certAndSealJObject.CertBase64.length != 0) {
					var certBase64 = certAndSealJObject.CertBase64;
					DisplayMessage("正在获取UKEY序列号");
					$('#ArrayNo').val(certSN);
					$('#Certificator').val(certBase64);
					var UserName = $('#UserName').val();
					var ArrayNo = $('#ArrayNo').val();
					var Certificator = $('#Certificator').val();
					var Remark = $('#Remark').val();
					var ukeySelect = $('#ukeySelect').val();
					if (UserName == '' || UserName == 'undefined') {
						layer.msg('用户名不能为空');
						return;
					}
					EncryptCertList((data) => {
						data = JSON.parse(data);
						let CertID = data.CertInfolist[0].CertID;
						//加密的字符串
						var str = JSON.stringify({
							User:{
								UserName: UserName, //用户名
								ArrayNo: ArrayNo, //不知道
								Certificator: Certificator, //不知道
								Remark: Remark, //备注
								ukeySelect: ukeySelect, //选的ukey
							},
							validatecode: "@ViewBag.ValidateCode"
						});
						console.log(str,"2222222222222222222222222222")
						var External = {
							CertID: CertID,
							//编码
							DataBase64: base64.encode(str)
						};
						ExternalEncrypt(JSON.stringify(External), (data) => {
							data = JSON.parse(data);
							if (data.error == "Ok") {
								var dataToPost = {
									// ArrayNo: CertID,
									EncryptData: data.contentBase64
								};
								DisplayMessage("正在向服务器发送验证数据");
								myPost("/User/Create", dataToPost, function(data) {
									console.log(data);
								});
							}
						});
					})





				}
			});



		});
	});
</script>
}
